package cn.tj.wenda.cloud.bas.rest.impl;

import cn.tj.wenda.cloud.bas.rest.BasTenantRest;
import cn.tj.wenda.cloud.bas.service.BasTenantService;
import cn.tj.wenda.cloud.bas.entity.BasTenant;
import cn.tj.wenda.cloud.entity.LoginUserInfo;
import cn.tj.wenda.cloud.entity.ResponseBean;
import cn.tj.wenda.cloud.exception.BusinessException;
import cn.tj.wenda.cloud.util.EntityUtil;
import cn.tj.wenda.cloud.util.JsonUtils;
import cn.tj.wenda.cloud.util.LoginUser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
@Scope("prototype")
public class BasTenantRestImpl implements BasTenantRest {
	
	private static Logger logger = LoggerFactory.getLogger(BasTenantRestImpl.class);

    @Autowired
    BasTenantService basTenantService;
    
    @Autowired
    EntityUtil entityUtil;
    
    @Value("${server.port}")
    String port;

    @Override
    public ResponseBean getBasTenantList(int page, int limit, String params) {
        ResponseBean res = new ResponseBean();
		LoginUserInfo loginUserInfo = LoginUser.getLoginUserInfo();
        logger.info(JsonUtils.objectToJsonStr(loginUserInfo));

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) servletRequestAttributes.getRequest();

        try {
        	PageInfo<BasTenant> pageInfo = basTenantService.getBasTenantList(page,limit,params);
        	entityUtil.addPageInfo(res, pageInfo);
        	res.setErrMsg("端口号5:" + port);//负载均衡测试
        }
        catch(BusinessException e) {
        	res.setErr("1");
        	res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
        	res.setErr("1");
        	res.setErr(ex.getLocalizedMessage());
        	logger.error("获取租户信息时出错",ex);
        }
        
        return res;
    }

    @Override
    public ResponseBean updateBasTenant(String tenantid,@RequestBody BasTenant basTenant) {
    	ResponseBean res = new ResponseBean();
        
        try {
        	int ret = basTenantService.updateBasTenant(tenantid, basTenant);
        	res.setData(ret);
        }
        catch(BusinessException e) {
        	res.setErr("1");
        	res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
        	res.setErr("1");
        	res.setErr(ex.getLocalizedMessage());
        	logger.error("更新租户信息时出错",ex);
        }
        
        return res;
    }

    @Override
    public ResponseBean insertBasTenant(@RequestBody BasTenant basTenant) {
    	ResponseBean res = new ResponseBean();
        
        try {
        	int ret = basTenantService.insertBasTenant(basTenant);
        	res.setData(ret);
        }
        catch(BusinessException e) {
        	res.setErr("1");
        	res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
        	res.setErr("1");
        	res.setErr(ex.getLocalizedMessage());
        	logger.error("新增租户信息时出错",ex);
        }
        
        return res;
    }

    @Override
    public ResponseBean deleteBasTenant(String tenantid) {
        return null;
    }
}
